package com.lwc.cfdns.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwc.cfdns.common.Assert;
import com.lwc.cfdns.enums.ResultCode;
import com.lwc.cfdns.pojo.bo.system.NoticeBO;
import com.lwc.cfdns.pojo.dto.system.NoticeDTO;
import com.lwc.cfdns.pojo.entity.SystemCate;
import com.lwc.cfdns.pojo.entity.SystemNotice;
import com.lwc.cfdns.pojo.vo.system.CateVO;
import com.lwc.cfdns.pojo.vo.system.NoticeVO;
import com.lwc.cfdns.service.SystemNoticeService;
import com.lwc.cfdns.mapper.SystemNoticeMapper;
import com.lwc.cfdns.utils.ConvertUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author spring
 * @description 针对表【cfdns_system_notice(通知公告表)】的数据库操作Service实现
 * @createDate 2023-06-05 14:24:11
 */
@Service
public class SystemNoticeServiceImpl extends ServiceImpl<SystemNoticeMapper, SystemNotice>
        implements SystemNoticeService {

    /**
     * 创建
     *
     * @param noticeDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/5 14:34:34
     */
    @Override
    public Map<String, Object> create(NoticeDTO noticeDTO) {
        SystemNotice systemNotice = new SystemNotice();
        BeanUtils.copyProperties(noticeDTO, systemNotice);
        this.baseMapper.insert(systemNotice);
        Map<String, Object> map = new HashMap<>();
        map.put("id", systemNotice.getId());
        return map;
    }

    /**
     * 修改
     *
     * @param noticeDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/5 14:36:02
     */
    @Override
    public Map<String, Object> update(NoticeDTO noticeDTO) {
        SystemNotice systemNotice = this.baseMapper.selectById(noticeDTO.getId());
        Assert.notNull(systemNotice, ResultCode.NOT_DATA_FOUNT);
        BeanUtils.copyProperties(noticeDTO, systemNotice, "created_at");
        Map<String, Object> map = new HashMap<>();
        int i = this.baseMapper.updateById(systemNotice);
        map.put("result", i);
        return map;
    }

    /**
     * 详情
     *
     * @param id
     * @return NoticeVO
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/5 14:39:06
     */
    @Override
    public NoticeVO view(Long id) {
        SystemNotice systemNotice = this.baseMapper.selectById(id);
        Assert.notNull(systemNotice, ResultCode.NOT_DATA_FOUNT);
        NoticeVO noticeVO = new NoticeVO();
        BeanUtils.copyProperties(systemNotice, noticeVO);
        return noticeVO;
    }

    /**
     * 删除
     *
     * @param ids
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/5 14:41:13
     */
    @Override
    public Map<String, Object> delete(String ids) {
        int i = 0;
        if (ids.contains(",")) {
            i = this.baseMapper.delete(new LambdaQueryWrapper<SystemNotice>().in(SystemNotice::getId, ConvertUtils.toLongArray(ids)));
        } else {
            i = this.baseMapper.deleteById(ConvertUtils.toLong(ids));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("result", i);
        return map;
    }

    /**
     * 列表
     *
     * @param noticeBO
     * @return IPage<SystemNotice>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/5 14:44:34
     */
    @Override
    public IPage<SystemNotice> lists(NoticeBO noticeBO) {
        Page<SystemNotice> page = this.baseMapper.selectPage(noticeBO.toPage(), noticeBO.toQueryWrapper());

        //转化为VO
        page.convert(s -> {
            NoticeVO noticeVO = new NoticeVO();
            BeanUtils.copyProperties(s, noticeVO);
            return noticeVO;
        });

        return page;
    }
}




